package models

import (
	"192.168.1.75/go-pkg/helper"
	"gorm.io/gorm"
)

// AlarmNotifyRecord 通知规则
type AlarmNotifyRecord struct {
	ID           string `json:"id" gorm:"column:id;primaryKey;type:int;size:64;comment:主键"`
	AlarmType    int    `json:"alarm_type" gorm:"column:alarm_type;type:int;size:32;comment:告警类型"`
	MerchantID   string `json:"merchant_id,omitempty" gorm:"column:merchant_id;type:int;size:64;comment:商户id"`
	RobotID      string `json:"robot_id" gorm:"column:robot_id;index:idx_createdAt_robotID,priority:2;type:string;size:40;comment:设备id"`
	ReceiveUsers string `json:"receive_users" gorm:"column:receive_users;type:string;size:10000:comment:接收用户"`
	Detail       string `json:"detail" gorm:"column:detail;type:string;size:200;comment:告警详情"`
	NotifyType   int    `json:"notify_type" gorm:"column:notify_type;type:int;size:8;comment:通知方式"`
	PushResult   int    `json:"push_result" gorm:"column:push_result;type:int;size:8;default:1;comment:推送结果"`
	CreatedAt    int64  `json:"created_at" gorm:"autoCreateTime;column:created_at;index:idx_createdAt_robotID,priority:1;type:int;size:64;comment:创建时间"`
}

// TableName 表名称
func (n *AlarmNotifyRecord) TableName() string {
	return "alarm_notify_record"
}

// BeforeCreate 自增id
func (n *AlarmNotifyRecord) BeforeCreate(tx *gorm.DB) (err error) {
	// id为空时，自动补全
	if n.ID != "" {
		return nil
	}
	uuid, err := helper.UUID{}.String()
	if err != nil {
		return err
	}
	n.ID = uuid
	return nil
}

// 模式自动注册
func init() {
	Models = append(Models, &AlarmNotifyRecord{})
}
